//
// Created by hp on 2020-01-01.
//

#include "Gameobject.h"

#include <utility>

Gameobject::Gameobject() {

}
Gameobject::Gameobject(vector<string> paths, string Sound,point point1, int w, int h,int v) {
    imagePaths=std::move(paths);
    pos=point1;
    wid=w;
    hei=h;
    volocity=v;
    is_first= true;
    texID=0;
    expSound=BASS_StreamCreateFile(FALSE, FileSystem::getPath(Sound).c_str(), 0, 0, 0);
    Gameobject::loadImage();

}
Gameobject::~Gameobject() {
}

void Gameobject::loadImage() {
    int width, height, nrChannels;
    // The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
    stbi_set_flip_vertically_on_load(true);
    for (int i = 0; i < imagePaths.size(); ++i) {
        unsigned char *data = stbi_load(FileSystem::getPath(imagePaths[i]).c_str(), &width, &height, &nrChannels, 0);
        data;
        unsigned int textureID;
        glGenTextures(1, &textureID);
        glBindTexture(GL_TEXTURE_2D,
                      textureID); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
        // set the texture wrapping parameters
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                        GL_REPEAT);    // set texture wrapping to GL_REPEAT (default wrapping method)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBindTexture(GL_TEXTURE_2D, textureID);
        if (data) {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
        } else {
            std::cout << "Failed to load texture" << std::endl;
        }
        stbi_image_free(data);
        textures.push_back(textureID);
    }

}

void Gameobject::drawImage(Shader ourShader) {
    float vertices[32];
    Game::convVert(pos.x, pos.y, wid, hei, vertices);
    unsigned int indices[] = {
            0, 1, 3, // first triangle
            1, 2, 3  // second triangle
    };
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    glBindTexture(GL_TEXTURE_2D, textures[texID]);
    ourShader.use();
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}

void Gameobject::move(char dir,GLfloat dt) {
    float dis=volocity*dt;
    switch (dir) {
        case 'W':
            if(pos.y<Game::Height-hei) {
                pos.y += dis;
            }else{
                pos.y=Game::Height-hei;
            }
            break;
        case 'S':
            if(pos.y>0) {
                pos.y -= dis;
            } else{
                pos.y=0;
            }
            break;
        case 'D':
            if(pos.x<Game::Width-wid) {
                pos.x += dis;
            } else{
                pos.x=Game::Width-wid;
            }
            break;
        case 'A':
            if(pos.x>0) {
                pos.x -= dis;
            } else{
                pos.x=0;
            }
    }
}

bool Gameobject::pause(GLfloat curTime, float pauTime,Shader ourShader) {
    if(is_first) {
        beginTime = curTime;
        is_first= false;
    }
    float deltaTime=curTime-beginTime;
    if(deltaTime<=pauTime) {
        Gameobject::drawImage(ourShader);
        return false;
    }
    is_first=true;
    return true;

}

bool Gameobject::explode(float curentTime,Shader ourShader) {
    if(!have_been_exploded){
        BASS_ChannelPlay(expSound, FALSE);
    }
    have_been_exploded= true;
    if(texID!=textures.size()-1){
        texID++;
        if(!Gameobject::pause(curentTime,0.15,ourShader)){
            texID--;

        }
    }
    else {
        return true;
    }
    return false;
}

void Gameobject::beingHurt(int harm) {
    HP-=harm;
    printf("%d\n",HP);
}